using streamsearcher;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

class Listing_01
{

    internal class Program
    {
        static readonly int buffer_size = 4096;
        private static void Main(string[] args)
        {
            //var patternString = @"3 ? 绥\;?; 憹j辯! ?";
            //var patternBytes = Encoding.ASCII.GetBytes(patternString);

            var patternBytes = new byte[]  {

                0xE7, 0x4C, 0x2B, 0x24, 0xB8, 0xE7, 0xB3, 0xCF, 0xD9, 0xF7, 0x9C, 0x5B, 0x75, 0x07, 0x89, 0xBE

        };
        
            var replaceBytes = new byte[]  {
                
                0x00, 0x00, 0x04, 0x3A, 0x00, 0x00, 0x05, 0x2D, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1D, 0x89, 0x24
            };
            var searcher = new StreamSearcher(patternBytes);

            //foreach (var fname in new[] { "C:\\Users\\leomojys\\Desktop\\test.txt" })
            foreach (var fname in new[] { "C:\\Users\\leomojys\\Desktop\\1.png" })
            {
                using var stream = File.Open(fname, FileMode.Open, FileAccess.ReadWrite);
                while (stream.Position < stream.Length)
                {
                    var position = searcher.Search(stream);

                    if (position > 0)
                    {
                        Console.WriteLine(position);
                        stream.Seek(stream.Position - patternBytes.Length, SeekOrigin.Begin);
                        stream.Write(replaceBytes, 0, replaceBytes.Length);
                    }
                }
                stream.Flush(true);
            }
        }
    }
}